package com.example.studyPlatform.service;

import com.example.studyPlatform.mapper.UserMapper;
import com.example.studyPlatform.bean.User;
import com.example.studyPlatform.service.protocol.LoginService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

@Service
public class LoginServiceImpl implements LoginService {
    UserMapper userMapper;

    public LoginServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public Optional<User> login(String username, String password) {
        Objects.requireNonNull(username);
        Objects.requireNonNull(password);
        return Optional.ofNullable(userMapper.login(username,password));
    }

    @Override
    public boolean logout(User user) {
        Objects.requireNonNull(user);
        user.setId(null);
        return true;
    }

    @Override
    public boolean isLogin(User user) {
        return  user!=null&&user.getId()!=null;
    }
}
